﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VO;

namespace DAL
{
    public class LogConsultaWebServiceDAL
    {
        #region Insert
        public void Insert(LogConsultaWebService obj)
        {
            var contexto = new CallcenterEntities();
            var objC = new tbCCLogConsultaWebService();
            objC = PopulaObjeto(obj, objC);
            contexto.AddTotbCCLogConsultaWebService(objC);
            contexto.SaveChanges();
        }
        #endregion

        #region Select
        public IList<LogConsultaWebService> Select(int idUsuario = 0)
        {
            var contexto = new CallcenterEntities();

            IQueryable<tbCCLogConsultaWebService> logConsultaWebServices;

            logConsultaWebServices = (from tb in contexto.tbCCLogConsultaWebService
                           where (tb.IdUsuario == idUsuario || idUsuario == 0)
                           select tb);

            //Carrega lista
            var list = PopulaObjeto(logConsultaWebServices);

            return list;
        }
        #endregion

        #region SelectAll
        public IList<LogConsultaWebService> SelectAll(int idUsuario = 0)
        {
            var contexto = new CallcenterEntities();

            IQueryable<tbCCLogConsultaWebService> logConsultaWebServices;

            logConsultaWebServices = (from tb in contexto.tbCCLogConsultaWebService
                          where (tb.IdUsuario == idUsuario || idUsuario == 0)
                          select tb);

            //Carrega lista
            var list = PopulaObjeto(logConsultaWebServices);

            return list;
        }
        #endregion

        #region PopulaObjeto
        protected static tbCCLogConsultaWebService PopulaObjeto(LogConsultaWebService obj, tbCCLogConsultaWebService tabela)
        {
            tabela.IdUsuario = obj.IdUsuario;
            tabela.WebService = obj.WebService;
            tabela.DataConsulta = DateTime.Now;
            tabela.XmlResposta = obj.strXml;
            tabela.CpfConsultado = obj.strCPF;
            return tabela;
        }

        protected static IList<LogConsultaWebService> PopulaObjeto(IQueryable<tbCCLogConsultaWebService> reader)
        {
            var list = new List<LogConsultaWebService>();
            foreach (var obj in reader)
            {
                var newObj = new LogConsultaWebService();
                newObj.WebService = obj.WebService;
                newObj.IdUsuario = (int)obj.IdUsuario;
                list.Add(newObj);
            }
            return list;
        }
        #endregion
    }
}
